//
//  TranslateToolCell.m
//  SJT
//
//  Created by xiaoLv on 16/11/8.
//  Copyright © 2016年 dnzx. All rights reserved.
//

#import "TranslateToolCell.h"

@implementation TranslateToolCell

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
}

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"LanguageCell";
    TranslateToolCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[TranslateToolCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    return cell;
}

//自适应文字宽度
+ (CGFloat)widthForLableText:(NSString*)text ForTextSize:(CGFloat)size
{
    CGRect rect = [text boundingRectWithSize:CGSizeMake(200,[ UIScreen mainScreen].bounds.size.height) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:size] } context:nil];
    return rect.size.width;
}
-(void)layoutSubviews{
    
    self.fromheaderImgV.frame = CGRectMake(10, 6, 35, 32);
    self.fromImgV.frame = CGRectMake(CGRectGetMaxX(self.fromheaderImgV.frame), 6, 93, 80);
    //宽度自适应
    CGFloat textLbWidth = [TranslateToolCell widthForLableText:self.textLb.text ForTextSize:14];
    self.textLb.frame =CGRectMake(65, 16, textLbWidth, 15);
//    self.textLb.backgroundColor = [UIColor cyanColor];
    
    self.toheaderImgV.frame = CGRectMake(self.frame.size.width-45, 6, 35, 32);
    self.toImgV.frame = CGRectMake(CGRectGetMaxX(self.toheaderImgV.frame)-35-93, 6, 93, 80);
    CGFloat rightTextLbWidth = [TranslateToolCell widthForLableText:self.rightTextLb.text ForTextSize:14];
    self.rightTextLb.frame =CGRectMake(self.toImgV.frame.origin.x+20, 16, rightTextLbWidth, 15);
    
    
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:_translateModel.srcText];
    UILabel *alabel=[[UILabel alloc]init];
    alabel.attributedText = attrStr;
    // 计算文本的大小
    CGSize textSize = [alabel.text boundingRectWithSize:CGSizeMake(200, MAXFLOAT) // 用于计算文本绘制时占据的矩形块
     options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading // 文本绘制时的附加选项
     attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:14],NSFontAttributeName, nil]// 文字的属性
      context:nil].size;
    
//    UITableViewCell *didSelectCell = [_messageTable  cellForRowAtIndexPath:index];
    if ([_translateModel.fromWhere isEqualToString:@"from"]) {
        UIImageView *changeImgV = _fromImgV;
        CGRect frame = changeImgV.frame;
//        frame.origin.x = ScreenWidth - textSize.width -45 -40;    //frame.origin.x-(textSize.width-70)
        
        
        if([_translateModel.msgType isEqualToString:@"audio"]){
            frame.size.width = textSize.width+50;
        }else{
             frame.size.width = textSize.width+40;//文字翻译的宽度
        }
        frame.size.height = textSize.height+30;
        changeImgV.frame = frame;
        
        
        UILabel *textLb = _textLb;
        frame = textLb.frame;
//        frame.origin.x = ScreenWidth - textSize.width -35 -40;    //frame.origin.x-(textSize.width-70)
        frame.size.height =textSize.height;
        textLb.frame = frame;
        
        _fromheaderImgV.hidden=NO;
        _fromImgV.hidden=NO;
        _textLb.hidden=NO;
        
        _toImgV.hidden=YES;
        _toheaderImgV.hidden=YES;
        _rightTextLb.hidden=YES;
    }else{
        UIImageView *changeImgV = _toImgV;
        CGRect frame = changeImgV.frame;
        frame.origin.x = self.frame.size.width - textSize.width -45 -40;    //frame.origin.x-(textSize.width-70)
        frame.size.width = textSize.width+45;
        
        if([_translateModel.msgType isEqualToString:@"audio"]){
            frame.size.width = textSize.width+45;
        }else{
            frame.size.width = textSize.width+35;
        }
         frame.size.height = textSize.height+30;
        changeImgV.frame = frame;
        
        UILabel *textLb = _rightTextLb;
        frame = textLb.frame;
        frame.origin.x = self.frame.size.width - textSize.width -35 -40;
           //frame.origin.x-(textSize.width-70)
        frame.size.height =textSize.height;
        textLb.frame = frame;
        
        _fromheaderImgV.hidden=YES;
        _fromImgV.hidden=YES;
        _textLb.hidden=YES;
        
        _toImgV.hidden=NO;
        _toheaderImgV.hidden=NO;
        _rightTextLb.hidden=NO;

    }
    _leftAudioImgV.frame =CGRectMake(CGRectGetMaxX(_textLb.frame) + 1, CGRectGetMaxY(_textLb.frame) - 18 , 15, 15);
    _rightAudioImgV.frame =CGRectMake(CGRectGetMaxX(_rightTextLb.frame)+1, CGRectGetMaxY(_rightTextLb.frame)- 18 , 15, 15);

}

/**
 *  cell的初始化方法，一个cell只会调用一次
 *  一般在这里添加所有可能显示的子控件，以及子控件的一次性设置
 */
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        // 点击cell的时候不要变色
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIImageView *fromheaderImgV = [[UIImageView alloc]init];
        fromheaderImgV.image = [UIImage imageNamed:@"Voice-RED-1"];
        [self.contentView addSubview:fromheaderImgV];
        self.fromheaderImgV = fromheaderImgV;
        
        UIImageView *fromImgV = [[UIImageView alloc]init];
//        fromImgV.image = [UIImage imageNamed:@"chat_from"];
        fromImgV.image = [[UIImage imageNamed:@"chat_from"] stretchableImageWithLeftCapWidth:52 topCapHeight:52];
        [self.contentView addSubview:fromImgV];
        self.fromImgV = fromImgV;
        
        UILabel *textLb = [[UILabel alloc]init];
        textLb.font=[UIFont systemFontOfSize:13];
        textLb.numberOfLines=0;
        textLb.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:textLb];
        self.textLb = textLb;
        
        UIImageView *toheaderImgV = [[UIImageView alloc]init];
        toheaderImgV.image = [UIImage imageNamed:@"Voice-BLU"];
        [self.contentView addSubview:toheaderImgV];
        self.toheaderImgV = toheaderImgV;
        
        UIImageView *toImgV = [[UIImageView alloc]init];
        toImgV.image = [[UIImage imageNamed:@"chat_to"] stretchableImageWithLeftCapWidth:52 topCapHeight:52];
//        toImgV.image = [UIImage imageNamed:@"chat_to"];
        [self.contentView addSubview:toImgV];
        self.toImgV = toImgV;
        
        UILabel *rightTextLb = [[UILabel alloc]init];
//        rightTextLb.backgroundColor = [UIColor cyanColor];
        rightTextLb.font=[UIFont systemFontOfSize:13];
        rightTextLb.numberOfLines=0;
        [self.contentView addSubview:rightTextLb];
        self.rightTextLb = rightTextLb;
        
        
        UIImageView *leftAudioImgV = [[UIImageView alloc]init];
        leftAudioImgV.image = [UIImage imageNamed:@"Voice-RED-1"];
        [self.contentView addSubview:leftAudioImgV];
        self.leftAudioImgV = leftAudioImgV;
        
        UIImageView *rightAudioImgV = [[UIImageView alloc]init];
        rightAudioImgV.image = [UIImage imageNamed:@"Voice-BLU"];
        [self.contentView addSubview:rightAudioImgV];
        self.rightAudioImgV = rightAudioImgV;
        
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


@end
